package com.yunxi.service.dubboService.impl.sto;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.yunxi.core.util.BeanTools;
import com.yunxi.model.sto.StoCarriageRoute;
import com.yunxi.model.sto.StoCarriageRouteDetail;
import com.yunxi.service.localService.sto.StoCarriageRouteDetailService;
import com.yunxi.service.localService.sto.StoCarriageRouteService;
import com.yunxi.sto.pojo.StoCarriageRouteDetailVO;
import com.yunxi.sto.pojo.StoCarriageRouteVO;
import com.yunxi.sto.service.StoCarriageRouteDubboService;
import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.List;

/**
 * <p>
 * 运输路线 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-05-15
 */
public class StoCarriageRouteDubboServiceImpl implements StoCarriageRouteDubboService {

    @Autowired
    @Qualifier(value = "stoCarriageRouteServiceImpl")
    StoCarriageRouteService stoCarriageRouteService;

    @Autowired
    @Qualifier(value = "stoCarriageRouteDetailServiceImpl")
    StoCarriageRouteDetailService stoCarriageRouteDetailService;

    /**
     * 获取单个
     * @param id
     * @return
     */
    @Override
    public StoCarriageRouteVO getById(String id) {
        StoCarriageRoute stoCarriageRoute = stoCarriageRouteService.getById(id);
        StoCarriageRouteVO stoCarriageRouteVO = new StoCarriageRouteVO();
        BeanTools.copyProperties(stoCarriageRouteVO,stoCarriageRoute);
        List<StoCarriageRouteDetailVO> list = stoCarriageRouteService.getCarrierById(id);
        stoCarriageRouteVO.setDetailList(list);
        return stoCarriageRouteVO;
    }

    @Override
    public void saveOrUpdate(StoCarriageRouteVO stoCarriageRouteVO) throws Exception {
        StoCarriageRoute stoCarriageRoute = new StoCarriageRoute();
        BeanTools.copyProperties(stoCarriageRoute,stoCarriageRouteVO);
        //判断路线编号不可重复添加
        checkCodeIsExsit(stoCarriageRouteVO);
        if(stoCarriageRoute.getRouteId() == null || StringUtil.isEmpty(stoCarriageRoute.getRouteId())){
            stoCarriageRoute.setApproveState("1");
        }
        else{
            //清空已保存的运输路线与承运商关系
            stoCarriageRouteService.removeRouteDetail(stoCarriageRoute.getRouteId());
        }
        //已审核通过的路线不允许编辑
        if(stoCarriageRoute.getRouteId() != null && stoCarriageRoute.getApproveState().equals(2)){
            throw new Exception("操作失败,已审核通过的路线不允许编辑!");
        }
        stoCarriageRouteService.saveOrUpdate(stoCarriageRoute);
        List<StoCarriageRouteDetailVO> detailList = stoCarriageRouteVO.getDetailList();

        if(detailList != null && detailList.size() > 0){
            for(StoCarriageRouteDetailVO stoCarriageRouteDetailVO : detailList){
                StoCarriageRouteDetail stoCarriageRouteDetail = new StoCarriageRouteDetail();
                BeanTools.copyProperties(stoCarriageRouteDetail,stoCarriageRouteDetailVO);
                stoCarriageRouteDetail.setRouteId(stoCarriageRoute.getRouteId());
                stoCarriageRouteDetailService.saveOrUpdate(stoCarriageRouteDetail);
            }
        }
    }

    /**
     * 检查路线代码是否重复添加
     * @param stoCarriageRouteVO
     * @throws Exception
     */
    private void checkCodeIsExsit(StoCarriageRouteVO stoCarriageRouteVO) throws Exception{
        if(stoCarriageRouteVO.getRouteCode() != null && StringUtils.isNotEmpty(stoCarriageRouteVO.getRouteCode())){
            QueryWrapper<StoCarriageRoute> queryWrapper = new QueryWrapper();
            if(stoCarriageRouteVO.getRouteId() != null && StringUtils.isNotEmpty(stoCarriageRouteVO.getRouteId())){
                queryWrapper.ne("ROUTE_ID",stoCarriageRouteVO.getRouteId());
            }
            queryWrapper.eq("ROUTE_CODE",stoCarriageRouteVO.getRouteCode());
            List<StoCarriageRoute> list = stoCarriageRouteService.list(queryWrapper);
            if(list != null && list.size() > 0){
                throw new Exception("操作失败,路线编号"+stoCarriageRouteVO.getRouteCode()+"已存在!");
            }
        }
    }

    @Override
    public void removeById(String id) {
    stoCarriageRouteService.removeById(id);
    }

    @Override
    public void del(List<String> list,String userId) {
        stoCarriageRouteService.delAndReset(list,1,userId);
    }

    @Override
    public void reset(List<String> list,String userId) {
        stoCarriageRouteService.delAndReset(list,0,userId);
    }

    @Override
    public List list() {
        return stoCarriageRouteService.getList();
    }

    @Override
    public List carrierList(String routeId,String carriageType) {
        return stoCarriageRouteService.carrierList(routeId,carriageType);
    }

    @Override
    public void approve(List<StoCarriageRouteVO> list,String userId) throws Exception{
        stoCarriageRouteService.approve(list,userId);
    }

    @Override
    public PageInfo<StoCarriageRouteVO> findPageByVO(StoCarriageRouteVO stoCarriageRouteVO, Integer pageNum, Integer pageSize) {
        PageInfo<StoCarriageRouteVO> pageInfo = stoCarriageRouteService.pageByVo(stoCarriageRouteVO, pageNum, pageSize);
        return pageInfo;
    }

}
